package com.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class Teacher extends Model<Teacher>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final Teacher dao = new Teacher();
	
	/**
	 * 根据老师姓名学号查询老师id
	 * @param tchName
	 * @param tchNum
	 * @return
	 */
	public Integer searchTchId(String tchName, String tchNum)
	{
		Integer tchId;
		String sql="SELECT teacher_id from teacher where teacher_no='"+tchNum+"' and teacher_name='"+tchName+"'";
		tchId=Teacher.dao.findFirst(sql).getInt("teacher_id");
		return tchId;
	}
	
	/**
	 * 根据相应的系编号查找教学秘书id
	 */
	public Integer selecSecId(int depId)
	{		
		String sql="SELECT * FROM teacher where teacher.teacher_department = "+depId+" and teacher.teacher_staff_category='教学秘书'";
		return Teacher.dao.findFirst(sql).getInt("teacher_id");
	}
	
	/**
	 * 根据教师Id查询相应系Id
	 * @param secId
	 * @return
	 */
	public Integer searchDepId(int tchId)
	{
		return Teacher.dao.findById(tchId).getInt("teacher_department");
	}
	
	/**
	 * 设置领导小组成员
	 * @param tchId
	 * @param category
	 * @return
	 */
	public Map<String, Object> setGroup(int tchId,String category)
	{
		boolean success=false;
		String msg="";
		Integer depId=Teacher.dao.searchDepId(tchId);
		Map<String, Object> m = new HashMap<String, Object>();
		if(category.equals("组长"))
		{
			String sql="select * from teacher where teacher_department="+depId+" and teacher_category='组长'";
			Teacher tch = Teacher.dao.findFirst(sql);
			if(tch==null)
				success=Teacher.dao.findById(tchId).set("teacher_category", category).update();
			else
				msg="已有组长，请重新设置！";
		}
		else
			success=Teacher.dao.findById(tchId).set("teacher_category", category).update();
		m.put("success", success);
		m.put("msg", msg);
		System.out.println("success:"+success);
		System.out.println("msg:"+msg);
		return m;
	}
	
	/**
	 * 删除领导小组成员
	 * @param tchId
	 * @return
	 */
	public boolean deleteGroup(int tchId)
	{
		boolean success=false;
		success=Teacher.dao.findById(tchId).set("teacher_category", null).update();
		return success;
	}
	
	public Page<Teacher> paginate(int pageNumber, int pageSize,int department) 
	{
		return paginate(pageNumber, pageSize, "select *", "from teacher where teacher_department='"+department+"' order by teacher_id asc");
	}
	
	public Teacher searchTch(String tchName,int department)
	{
		Teacher teacher;
		String sql="select * from teacher where teacher_name = '"+tchName+"' and teacher_department='"+department+"'";
		teacher=Teacher.dao.findFirst(sql);
		return teacher;
	}
	
	public boolean setEvery_number(int number,int department)
	{
		boolean sign=false;
		String sql="UPDATE teacher  set teacher_num="+number+" where teacher_department='"+department+"'";
		int i=Db.update(sql);
		if(i>0)
		{
			sign=true;
		}
		return sign;
	}
	
	public boolean setTch_number(int id,int number)
	{
		boolean sign=false;
		sign=Teacher.dao.findById(id).set("teacher_num", number).update();
		return sign;
	}
	
	public List<Teacher> choseTeacher(int departmentId)
	{
		String sql="SELECT teacher_id,teacher_name from teacher WHERE teacher_chose >0 and teacher_department='"+departmentId+"' order by teacher_id asc";
		List<Teacher> chose_teacher=Teacher.dao.find(sql);
		return chose_teacher;
	}
	
	public Page<Teacher> chooseTeacher(int pageNumber, int pageSize,int departmentId)
	{
		return paginate(pageNumber, pageSize, "select *", "from teacher WHERE teacher_chose >0 and teacher_department='"+departmentId+"' order by teacher_id asc");
	}
	
	public boolean decreaseTchNum(int teacher_id)
	{
		boolean sign=false;
		int number=Teacher.dao.findById(teacher_id).getInt("teacher_chose");
		System.out.println(number);
		sign=Teacher.dao.findById(teacher_id).set("teacher_chose", number-1).update();
		return sign;
	}
	
	public List<Teacher> tchChose(int departmentId)
	{
		List<Teacher> tchList;
		String sql="SELECT * FROM teacher WHERE teacher_chose > 0 and teacher_department='"+departmentId+"'";
		tchList=Teacher.dao.find(sql);
		return tchList;
	}
}
